<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
class User extends Base
{
    private $mod;
    protected $beforeActionList = [
      'beforeEdit' => 'add,edit'
    ];
    public function _initialize() {
        parent::_initialize();
        $this->mod = model('user');
    }


    public function _search(){
        $map = array();
        $time_start = input('get.time_start');
        $time_end = input('get.time_end');
        if($time_start && !$time_end){
            $map['reg_time'] = ['egt',strtotime($time_start)];
        }elseif($time_end && $time_end){
            $map['reg_time'] = ['between',[strtotime($time_start),strtotime($time_end)+86399]];
        }
        ($keyword = input('get.keyword')) && $map['username'] = ['like', '%'.$keyword.'%'];
       // $cate_id = input('get.cate_id');
        ($model = input('get.model/d')) && $map['model'] = $model;
        $status = -1;
        if(isset($_GET['status']) && $_GET['status'] !== ''){
            $status = input('get.status/d');
        }
        $status>=0 && $map['status'] = ['eq',$status];
        $this->assign('modelArr',$this->mod->modelArr());
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
           // 'cate_id' => $cate_id,
            'model'   => $model,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }
    public function beforeEdit(){
        $modelArr = $this->mod->modelArr();
        $this->assign('modelArr',$modelArr);
    }
    public function addDo(){
        \app\common\model\User::event('after_insert',function($obj){
            $info_data = input('post.data/a');
            $obj->userInfo()->save($info_data);
        });
        parent::addDo();
    }
    public function editDo(){
        \app\common\model\User::event('before_update',function($obj){
            if(empty($obj->password)){
                unset($obj->password);
            }
            return true;
        });
        \app\common\model\User::event('after_update',function($obj){
            $info_data = input('post.data/a');
            $obj->userInfo->save($info_data);
            return true;
        });
        parent::editDo();
    }
    public function delete(){

        \app\common\model\User::event('before_delete',function($obj){
            //不能删除商家用户
            $model = $obj->getData('model');
            if($model == 2){
                return false;
            }
            return true;
        });
        \app\common\model\User::event('after_delete',function($obj){
            //删除用户资料
            $obj->userInfo->delete();
        });
        parent::delete();
    }
    public function ajaxCheckUser(){
        $username = input('param.username');
        $mobile   = input('param.mobile');
        $reg      = '/^1[34578][0-9]{9}$/';
        if($username){
            $map['username'] = $username;
        }elseif($mobile){
            if(preg_match($reg,$mobile)){
                $map['mobile'] = $mobile;
            }else{
                $this->ajaxReturn(0);
            }
        }else{
            $this->ajaxReturn(0);
        }
        $total = $this->mod->where($map)->count('id');
        if($total){
            $this->ajaxReturn(0);
        }else{
            $this->ajaxReturn(1);
        }
    }
    public function publicSelectUser(){

        $type  = input('param.type');//选择框类型radio checkbox
        $pagesize = 60;
        $total = $this->mod->where('status',1)->count();
        $totalPage = ceil($total/$pagesize);

        $this->assign('type',$type);
        $this->assign('totalPage',$totalPage);
        $this->ajaxReturn(1,'',$this->fetch());
    }
    public function ajaxGetList(){
        $pagesize = 60;
        $page = input('param.page/d',1);
        $keyword = input('param.keyword');
        $model   = input('model/d',0);
        $map['status'] = 1;
        if($keyword){
            $map['username'] = ['like','%'.$keyword.'%'];
        }
        $model && $map['model'] = $model;
        $lists =  $this->mod->field('id,username')->where($map)->page($page,$pagesize)->select();
        if($lists){
            $this->ajaxReturn(1,'',$lists);
        }else{
            $this->ajaxReturn(0,'无相关数据');
        }
    }
}